package com.learn.core.controller.manager;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.*;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LDict;
import com.learn.core.domain.LDictType;
import com.learn.core.dto.LDictDTO;
import com.learn.core.service.ILDictService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @auther CodeGenerator
 * @create 2021-03-13 19:39:25
 * @describe 字典表前端控制器
 */
@Api("数据字典")
@RestController
@RequestMapping("/manager/dict")
public class LDictController extends WebController<LDict> {

    @Autowired
    private ILDictService ilDictService;

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LDict> list(@RequestBody LDictDTO lDictDTO){
        try {
            if(lDictDTO.getPage()==null||lDictDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lDictDTO.getPage(), lDictDTO.getRows());
            QueryWrapper<LDict> queryWrapper = new QueryWrapper<>();
            if(lDictDTO.getDictTypeId()!=null)
                queryWrapper.eq("dict_type_Id", lDictDTO.getDictTypeId());
            if(StringUtils.isNotEmpty(lDictDTO.getStutes()))
               lDictDTO.setStutes("1");
            if (StringUtils.isNotEmpty(lDictDTO.getSysCode()))
                queryWrapper.eq("sys_code", lDictDTO.getSysCode());

            if(lDictDTO.getSortType()!=null&&lDictDTO.getSortBy()!=null)
                queryWrapper.orderBy(true,lDictDTO.getSortType(),lDictDTO.getSortBy());

            Page<LDict> lDicts = (Page<LDict>) ilDictService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lDicts)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(lDicts.getTotal(), lDicts, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
        }
    }


    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加字典", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LDict lDict, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lDict!=null) {
                if (StringUtils.isEmpty(lDict.getDictName())||lDict.getDictTypeId()==null) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_DICT_NAME);
                }
                if(StringUtils.isEmpty(lDict.getStutes()))
                    lDict.setStutes("1");
                String DictCode = CnToSpell.getPinYinHeadChar(lDict.getDictName());
                lDict.setDictCode(DictCode);
                lDict.setCreateTime(new Date());
                lDict.setCreateUser(userId);
                if (ilDictService.save(lDict)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除字典", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LDict lDict){
        try {
            if(lDict!=null){
                if(ilDictService.removeById(lDict.getDictId())){
                    return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e) {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新字典", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LDict lDict, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lDict!=null){
            lDict.setUpdateTime(new Date());
            lDict.setUpdateUser(userId);
            if(ilDictService.updateById(lDict)){
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}

